{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.6604, -0.7471, -0.1990],\n",
      "        [ 0.0926,  0.0060,  0.5541]])\n",
      "tensor([[-0.6604, -0.7471, -0.1990],\n",
      "        [ 0.0926,  0.0060,  0.5541]])\n"
     ]
    },
    {
     "ename": "RuntimeError",
     "evalue": "output with shape [1, 3] doesn't match the broadcast shape [2, 3]",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-a4fa25042c92>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mM\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mN\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mM\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msub_\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mN\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mM\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mRuntimeError\u001b[0m: output with shape [1, 3] doesn't match the broadcast shape [2, 3]"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "M=torch.rand(1,3)\n",
    "N=torch.rand(2,1)\n",
    "print(M-N)\n",
    "\n",
    "print(torch.sub(M,N))\n",
    "\n",
    "M.sub_(N)\n",
    "print(M)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.0031, -0.0002],\n",
      "        [-0.0050,  0.0135],\n",
      "        [ 0.0105,  0.0010]])\n",
      "tensor([[-0.0013,  0.0051],\n",
      "        [-0.0008, -0.0007],\n",
      "        [ 0.0030,  0.0135],\n",
      "        [ 0.0099,  0.0192]])\n",
      "tensor([[-0.0013, -0.0008,  0.0030,  0.0099],\n",
      "        [ 0.0051, -0.0007,  0.0135,  0.0192]])\n",
      "tensor([[ 2.8789e-06,  2.6929e-06, -1.2767e-05, -3.5397e-05],\n",
      "        [ 7.6108e-05, -5.3099e-06,  1.6724e-04,  2.1020e-04],\n",
      "        [-9.2001e-06, -9.2431e-06,  4.5005e-05,  1.2267e-04]])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "P=torch.normal(0,0.01,(3,2))\n",
    "print(P)\n",
    "Q=torch.normal(0,0.01,(4,2))\n",
    "print(Q)\n",
    "Q.t_()\n",
    "print(Q)\n",
    "print(torch.mm(P,Q))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(2.)\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "x=torch.tensor(1.,requires_grad=True)\n",
    "y1=x*x\n",
    "with torch.no_grad():\n",
    "    y2=x*x*x\n",
    "y3=y1+y2\n",
    "out=y3.mean()\n",
    "out.backward()\n",
    "print(x.grad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
